package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.CommentsMapper;
import com.zretc.dao.OrdersMapper;
import com.zretc.entity.Comments;
import com.zretc.entity.Orders;
import com.zretc.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author  吴凯
 */
@Service
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Override
    public List<Comments> getRootCommentsByFilmId(Integer filmId) {
        return commentsMapper.findRootCommentsByFilmId(filmId);
    }

    @Override
    public PageInfo<Comments> getAllRootComment(Map<String,String> param) {
        int pageNum =  Integer.parseInt(param.get("pageNum"));
        int pageSize =  Integer.parseInt(param.get("pageSize"));
        String accountName = param.get("accountName");
        String commentsContent = param.get("commentsContent");
        String commentsScore = param.get("commentsScore");

        // 开始分页
        PageHelper.startPage(pageNum,pageSize);


        Map<String, String> map = new HashMap<>();
        if("".equals(accountName) || accountName == null){
            map.put("accountName",null);
        }else{
            map.put("accountName","%" + accountName + "%");
        }

        if("".equals(commentsContent) || commentsContent == null){
            map.put("commentsContent",null);
        }else{
            map.put("commentsContent","%" + commentsContent + "%");
        }

        if("".equals(commentsScore) || commentsScore == null){
            map.put("commentsScore",null);
        }else{
            map.put("commentsScore",commentsScore);
        }

        List<Comments> comments = commentsMapper.selectAllRootComment(map);

        // 3.包装到PageInfo对象
        PageInfo<Comments> pageInfo = new PageInfo<>(comments);
        return pageInfo;
    }

    @Override
    public Integer deleteRootComment(List<String> comentsId) {
        return commentsMapper.deleteRootComment(comentsId);
    }

    @Override
    public Integer addRootComment(Comments comments) {
        // 判断是否卖票
        List<Orders> list = ordersMapper.getOrdersByUserAndFilm(comments.getUserId(), comments.getFilmId());
        if(list.size() == 0){
            return 0;
        }
        return commentsMapper.addRootComment(comments);
    }
}
